package cn.yj.core.business.service;

import cn.yj.cgs.dba.entity.UsedRemark;
import cn.yj.cgs.dba.mapper.UsedRemarkMapper;
import cn.yj.commons.utils.StringUtils;
import cn.yj.params.check.annotation.CheckObjectValue;
import cn.yj.params.check.annotation.KeyValue;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 永健
 * @since 2021-10-17 14:15
 */
@Service
public class UsedRemarkService extends ServiceImpl<UsedRemarkMapper, UsedRemark> {


    @CheckObjectValue(keyValue = @KeyValue(type = Map.class, name = {"userCode", "type", "schoolName"}))
    public List<String> getOftenUsedRemark(Map<String, Object> params) {
        return baseMapper.selectOftenUsedRemark(params).stream().map(UsedRemark::getContent).collect(Collectors.toList());
    }


    @CheckObjectValue(keyValue = @KeyValue(type = UsedRemark.class, name = {"content", "userCode", "schoolName",
            "type"}))
    @Override
    public boolean save(UsedRemark entity) {
        UsedRemark usedRemark =
                baseMapper.selectOne(new QueryWrapper<UsedRemark>().lambda().eq(UsedRemark::getContent,
                        entity.getContent()).eq(UsedRemark::getUserCode, entity.getUserCode()));
        if (StringUtils.isNotNull(usedRemark)) {
            usedRemark.setCount(usedRemark.getCount() + 1);
            return super.updateById(usedRemark);
        }
        return super.save(entity);
    }
}
